package com.catarina.framework.conceptual;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;

public class Example {
    public static void main(String argument[]) {
        Context cx = Context.enter();
        String content= "";
        try {
            Scriptable scope = cx.initStandardObjects();

            // Collect the arguments into a single string.
            content = "x= 'nullo'; function f ( a, b) { c= false; if (a == b) c= true ; return c;}";

            // Now evaluate the string we've collected. We'll ignore the result.
            cx.evaluateString(scope, content, "<cmd>", 1, null);

            // Print the value of variable "x"
            Object x = scope.get("x", scope);
            if (x == Scriptable.NOT_FOUND) {
                System.out.println("x is not defined.");
            } else {
                System.out.println("x = " + Context.toString(x));
            }

            // Call function "f('myArg')" and print its result.
            Object fObj = scope.get("f", scope);
            if (!(fObj instanceof Function)) {
                System.out.println("f is undefined or not a function.");
            } else {
                Object functionArgs[] = { "1" , "1"};
                Function f = (Function)fObj;
                Object result = f.call(cx, scope, scope, functionArgs);
                String report = "f('a, b') = " + Context.toString(result);
                System.out.println(report);
            }
        } finally {
            Context.exit();
        }
    }
}
